package com.example.springmvc_adv.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Map<String, Object> map = new HashMap<>();

    //按设置的拦截规则，必须登录才能访问
    @RequestMapping("/1")
    public Object u1(){
        return map;
    }

    //操作Session：先模拟登录，设置一个用户会话
    //按设置的拦截规则，不需要登录就能访问
    @RequestMapping("/login")
    public Object login(HttpServletRequest req){
        //模拟登录成功，登录的用户叫abc
        HttpSession session = req.getSession();//服务端会话管理的数据结构Map.put(随机字符串, session); return session
        session.setAttribute("user", "abc");//session对象也是map对象
        Map<String, Object> map = new HashMap<>();
        map.put("user", "abc");
        return map;
    }
}